package com.by.demo;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.by.util.DruidUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) {

          /*
    1. 定义配置文件 `druid.properties`
    2. 通过Properties集合加载配置文件
    3. 通过Druid连接池工厂类 获取数据库连接池对象
    4. 获取连接
     */
        //1、 通过Properties集合加载配置文件
        Connection conn=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        Properties prop=new Properties();
        try {
//            prop.load(DruidDemo.class.getClassLoader().getResourceAsStream("druidConfig.properties"));
//            DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

           conn = DruidUtils.getConnection();
             pstm = conn.prepareStatement("select * from student");
             rs = pstm.executeQuery();
            while (rs.next()){
                System.out.println(rs.getInt("sid") + "\t" + rs.getString("name") + "\t" + rs.getInt("age") + "\t" + rs.getDate("birthday"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                pstm.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }


}
